<?php

global $wpdb;


/*
delete file
*/
if(isset($_GET['fid']))
{
	if(nmFileUploader::deleteFile($_GET['fid']))
	{
		echo "<h3 style=\"color:green\" id=\"response\">File Deleted Successfully</h3>";
	}
	
}


/*
save file
*/
if(isset($_POST['nm-upload-name']))
{
	nmFileUploader::$name 		= $_POST['nm-upload-name'];
	nmFileUploader::$file_name 	= $_POST['file-name'];
	nmFileUploader::$notes		= $_POST['nm-notes'];
	
	if(nmFileUploader::saveFile())
	{
		echo "<h3 style=\"color:green\" id=\"response\">File Uploaded Successfully</h3>";
	}
	
}

/*$wpdb->show_errors();
$wpdb->print_error(); */

$arrFiles = nmFileUploader::getUserFiles();
//print_r($arrFiles);
?>
<div style="margin:5px;padding:5px;border:1px solid #CCC; background-color:#f5f5f5">
<div style="background-color:#FC6; font-size:10px">
<h3>Instructions:</h3>
<ol>
<li> 
Click <strong>Select Files</strong> and browse to the file you want to upload. Whilst you wait for it to load, type in a file name and additional notes. </li>
<li>
When the file is loaded click 'Upload'.
</li>
<li>
  You can download or delete the uploaded file(s) below.
</li>
</ol>
<p style="padding:5px; font-size:12px">
  <strong>Please Note: Only upload file names without spaces as we will not be able to download or view files with spaces. Thank you.</strong></p>
</div>
<br />

<div id="notices" style="color:red"></div>
<form action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI'])?>" method="post" onSubmit="return validate()">
<input type="hidden" name="file-name" id="file-name">
<table width="100%" cellspacing="2" cellpadding="2" style="font-size:.7em;border:none;padding:0px;margin:0px;">
  <tbody><tr>
    <td>File name:</td>
    <td><input type="text" name="nm-upload-name" id="nm-upload-name"></td>
  </tr>
  <tr>
    <td>File upload:</td>
    <!--<td>
    <div id="file-uploader">		
		<noscript>			
			<p>Please enable JavaScript to use file uploader.</p>
		
       	</noscript>         
		</div>
        
        <div id="nm_progressbar"><div id="runner"></div></div>
        <div id="progress"><ul></ul></div>
        <div id="fileNames">
            <ul>
            </ul>
        </div>	
       
							</td>-->
                            
    <td>
    <input id="file_upload" name="file_upload" type="file" />
    <span style="display:none" id="upload-response"></span>
    </td>
  </tr>
  <tr>
    <td>Additional notes:</td>
    <td><textarea name="nm-notes" style="width:90%;height:70px" class="" id="nm-notes"></textarea></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>
						<div class="dlg_change_indicator_button"><input type="submit" value="Upload" name="submit" onclick="dlg_change_indicator()" id="nm-upload"></div>
						<div id="working-area" style="display:none">
                        <?php
							echo "<img src=".plugins_url( 'images/loading.gif' , __FILE__)." />";
						?>
                        </div>	
							</td>
  </tr>
</tbody></table>
</form>

<br>
<div class="user-uploaded-files">
<h2>Uploaded Files</h2>
<table width="100%" border="0" id="user-files">
<thead>
	<tr>
        <th width="241" valign="middle"><strong>File Name</strong></th>
        <th width="534" valign="middle"><strong>Notes</strong></th>
        <th width="85" align="center" valign="middle"><strong>Date</strong></th>
        <th width="86" align="center" valign="middle"><strong>Action</strong></th>
      </tr>
</thead>


<tbody>
<?php foreach($arrFiles as $file):
if($_GET['fid'] == '')
	$urlDelete = str_replace( '%7E', '~', $_SERVER['REQUEST_URI']).'?fid='.$file -> id;
else
	$urlDelete = str_replace( '%7E', '~', $_SERVER['REQUEST_URI']).'&fid='.$file -> id;


$fileName = ABSPATH. 'wp-content/plugins/plugin-fileuploader/uploads/'.	$file -> file;

if(file_exists($fileName))		// if file exisits
{

	$urlDwnld = str_replace( '%7E', '~', $_SERVER['REQUEST_URI']).'?dwnld='.$file -> file;

?>
  <tr>
    <td><?= $file -> name?></td>
    <td><?= $file -> notes?></td>
    <td width="85" align="center"><?= date('d-M,y', strtotime($file -> date))?></td>
    <td width="86" align="center">
    <a href="<?= $urlDwnld?>">
	<?php echo "<img border=\"0\" src=".plugins_url( 'images/down_16.png' , __FILE__)." />";	?>
    </a>
     | 
    <a href="javascript:confirmFirst('<?= $urlDelete?>')">
	<?php echo "<img border=\"0\" src=".plugins_url( 'images/delete_16.png' , __FILE__)." />";	?>
    </a></td>
  </tr>
<?php 
	}	// if file exisits
endforeach;?>
  
</tbody>
</table>
</div>
<div style="clear:both"></div>
</div>
<em><strong>Existing Users Log in/out:</strong></em>
<script type="text/javascript">
	/* valums: iniailizing uploader */
		//createUploader('<?php echo plugins_url('', __FILE__);?>');
	/* valums: finishing uploader */
	
	//uploadify
	jQuery('#file_upload').uploadify({
    'uploader'  : '<?php echo plugins_url('js/uploadify/uploadify.swf', __FILE__);?>',
    'script'    : '<?php echo plugins_url('js/uploadify/uploadify.php', __FILE__);?>',
    'cancelImg' : '<?php echo plugins_url('js/uploadify/cancel.png', __FILE__);?>',
    'folder'    : '/wp-content/plugins/plugin-fileuploader/uploads',
    'auto'      : true,
	'onComplete'  : function(event, ID, fileObj, response, data) {
      //alert('There are ' + fileObj.name);
	  jQuery("#file-name").attr("value", fileObj.name);
	  jQuery("#upload-response").html(fileObj.name + ' loaded, Please now click \'Upload\' to upload the file').fadeIn(200);
    }
  });

</script>
